#!/usr/bin/python
import hashlib , os 
file_log="/tmp/file_monitor"

if not os.path.exists(file_log):

    os.mknod(file_log)

f = open(file_log) 

read_list=[]

read_dict={}

while 1: 

    file = f.readline().strip("\n") 

    if file: 

            read_list.append(file.split("\t"))
    else: 
            break
f.close()

read_dict=dict(read_list)

file_yuanzu=("/etc/passwd","/etc/hosts")

def md5Checksum(filePath):

    with open(filePath, 'rb') as fh:

        m = hashlib.md5()

        while True:

            data = fh.read(8192)

            if not data:

                break

            m.update(data)

        return m.hexdigest()


_dict = { }
for _f in file_yuanzu:                                                                                                   
                                                                                                                                  
    _dict["file_" + os.path.basename(_f)]=md5Checksum(_f) 

    f1 = open(file_log,'a') 

    f1.write("file_"+os.path.basename(_f)+'\t')

    f1.write(md5Checksum(_f)+"\n")

    f1.close()

for i in _dict.keys():

    _value = _dict.get(i)

    Read_value = read_dict.get(i)

    if _value != Read_value:

        _dict[i]="0"

    else:

        _dict[i]="1"
     

